import * as util from '../common/util.js'

function postAjax(arams, request, callback) {
	util.postAjax(arams, request, function(data){
		// console.log(arams + ':' + JSON.stringify(data));
		if(data.code==undefined){
			// uni.removeStorageSync('token')
			// uni.reLaunch({
			// 	url: '/pages/login/login'
			// })
		}else{
			if (data.code == 401) {
				let oinfo = {}
				if (!uni.getStorageSync('logintype')) {
					uni.showToast({
						title: '您还未登录，请登录',
						icon: 'none'
					})
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else {
					if (uni.getStorageSync('logintype') == 1) {
			
						oinfo = uni.getStorageSync('loginfo')
						util.postAjax('user/AppLogin', oinfo, function(res) {
							if (data.code == 0) {
								let userInfo = data.data
								uni.setStorageSync('token', userInfo.token);
								uni.setStorageSync('userinfo', userInfo);
								let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
								let curRoute = routes[routes.length - 1].route
								uni.reLaunch({
									url: '/'+curRoute
								})
							} else {
								uni.showToast({
									title: '您还未登录，请登录',
									icon: 'none'
								})
								uni.removeStorageSync('token')
								uni.removeStorageSync('userinfo')
								uni.removeStorageSync('loginfo')
								uni.removeStorageSync('logintype')
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}
						})
					}
					
					if (uni.getStorageSync('logintype') == 2) {				
			
						postAjax('User/wxLogin', {
							unionid: uni.getStorageSync('userinfo').weixin_unionid,
							channel_id: 2
						}, function(data) {
							if (data.code == 0) {
								uni.setStorageSync('token', data.data.token);
								uni.setStorageSync('userinfo', data.data);
								uni.setStorageSync('logintype', 2);
								uni.getSystemInfo({
									success: function(res) {
										uni.setStorageSync('application_client_type', res.platform);
									},
								})
								let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
								let curRoute = routes[routes.length - 1].route
								uni.reLaunch({
									url: '/'+curRoute
								})
							}else{
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}
						})
					}
				}
			
			}else {
			
				callback(data)
			}
		}
		
	}, function() {
		uni.reLaunch({
			url: '/pages/login/login'
		})
	});

}

export default postAjax;
